﻿using BlazorHero.CleanArchitecture.Application.Features.Members.Commands.AddEdit;
using BlazorHero.CleanArchitecture.Application.Features.Members.Commands.Delete;
using BlazorHero.CleanArchitecture.Application.Features.Members.Queries.GetAllPaged;
using BlazorHero.CleanArchitecture.Shared.Constants.Permission;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Polly;
using System.Threading.Tasks;

namespace BlazorHero.CleanArchitecture.Server.Controllers.v1.Members
{
    public class MemberController : BaseApiController<MemberController>
    {
        /// <summary>
        /// Get All Members
        /// </summary>
        /// <param name="pageNumber"></param>
        /// <param name="pageSize"></param>
        /// <param name="searchString"></param>
        /// <param name="orderBy"></param>
        /// <returns>Status 200 OK</returns>
        [Authorize(Policy = Permissions.Products.View)]
        [HttpGet]
        public async Task<IActionResult> GetAll(int pageNumber, int pageSize, string searchString, string orderBy = null)
        {
            var members = await _mediator.Send(new GetAllMembersQuery(pageNumber, pageSize, searchString, orderBy));
            return Ok(members);
        }

        ///// <summary>
        ///// Get a Product Image by Id
        ///// </summary>
        ///// <param name="id"></param>
        ///// <returns>Status 200 OK</returns>
        //[Authorize(Policy = Permissions.Products.View)]
        //[HttpGet("image/{id}")]
        //public async Task<IActionResult> GetProductImageAsync(int id)
        //{
        //    var result = await _mediator.Send(new GetProductImageQuery(id));
        //    return Ok(result);
        //}

        /// <summary>
        /// Add/Edit a Member
        /// </summary>
        /// <param name="command"></param>
        /// <returns>Status 200 OK</returns>
        [Authorize(Policy = Permissions.Members.Create)]
        [HttpPost]
        public async Task<IActionResult> Post(AddEditMemberCommand command)
        {
            return Ok(await _mediator.Send(command));
        }

        /// <summary>
        /// Delete a Member
        /// </summary>
        /// <param name="id"></param>
        /// <returns>Status 200 OK response</returns>
        [Authorize(Policy = Permissions.Members.Delete)]
        [HttpDelete("{id}")]
        public async Task<IActionResult> Delete(long id)
        {
            return Ok(await _mediator.Send(new DeleteMemberCommand { Id = id }));
        }

        ///// <summary>
        ///// Search Products and Export to Excel
        ///// </summary>
        ///// <param name="searchString"></param>
        ///// <returns>Status 200 OK</returns>
        //[Authorize(Policy = Permissions.Products.Export)]
        //[HttpGet("export")]
        //public async Task<IActionResult> Export(string searchString = "")
        //{
        //    return Ok(await _mediator.Send(new ExportProductsQuery(searchString)));
        //}
    }
}
